#pragma once
#include<string>
#include<unordered_map>
#include<sys/types.h>
#include<sys/socket.h>
//static string Line_break = "\n";
using namespace std;
//工具类
class Util
{
    public:
    static unordered_map<int,string> map_code_string;//状态码与状态码描述的映射
    static unordered_map<string,string> map_type;//后缀的对应表关系
    static void map_init() //对状态码映射进行初始化
    {
// #define OK 200
// #define NOT 404        
        map_code_string[200] = "OK";
        map_code_string[404] = "Not Found";
        //map_code_string[OK] = "Ok";
        //map_code_string[OK] = "Ok";
    }

    static void InitSuffix()
    {
        //建立映射关系
        map_type[".html"] = "text/html";
        map_type[".css"] = "text/css";
        map_type[".txt"] = "text/plain";
        map_type[".png"] = "image/png";
        map_type[".jpg"] = "image/jpg";
        map_type[".gif"] = "image/gif";

    }

    //按行分割
    static int ReadLien(int sock,std::string& out)
    {
        // 按  /r  || /n  ||  /r/n    三种可能的格式分割
        char ch = '`';
        while(ch != '\n')
        {
            ssize_t s = recv(sock,&ch,1,0);
            if(s == 1)
            {
                if(ch == '\r')
                {
                    recv(sock,&ch,1,MSG_PEEK);
                    if(ch == '\n')
                    {
                        recv(sock,&ch,1,0);
                    }
                    else{
                        ch = '\n';
                    }

                }
                out += ch;
            }
            else if(s == 0)
            {
                return 0;
            }
            else {
                return -1;
            }
        }
        return out.size();
    }
    //将一个字符串以空格切分成两串(k,val)
    //分割成功返回true
    static bool CutString(const std::string& s,std::string& k_out,std::string& val_out,const char p) //p是以什么字符分割
    {
        int t = 0; //开关,为0的时候赋值给k,遇到空格变成1,赋值给val
        for(size_t i = 0;i < s.size(); i ++)
        {
            if(s[i] == p)
            {
                t = 1;
                continue;
            }
            if(!t) k_out += s[i];
            else val_out += s[i];
        }
        if(!t) return false;
        
        //k_out.resize(k_out.size());
        return true;
    }
};
unordered_map<int,string> Util::map_code_string;
unordered_map<string,string> Util::map_type;